package cn.itcast.service.company.impl;

import cn.itcast.dao.company.CompanyDao;
import cn.itcast.domain.compony.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.utils.PageResult;
import cn.itcast.utils.UUIDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    CompanyDao companyDao;

    @Override
    public List<Company> findAll() {
        //System.out.println("service");
        return companyDao.findAll();
    }

    @Override
    public Company findById(String id) {
        return companyDao.findById(id);
    }

    @Override
    public void save(Company company) {
        //给公司设置id
        company.setId(UUIDUtils.getId());
        companyDao.save(company);
    }

    @Override
    public void update(Company company) {
        companyDao.update(company);
    }

    @Override
    public void deleteById(String id) {
        companyDao.deleteById(id);
    }

    //传统分页
   /* public PageResult findByPage(int page, int size) {
        //- 调用dao查询总记录数
        long total = companyDao.findTotal();
        //- 调用dao查询当前页的数据集合
        List<Company> list = companyDao.findByPage((page-1)*size,size);
        //- 封装PageResult 返回
        return new PageResult(total,list,page,size);
    }*/


    //pageHelper完成分页
    public PageInfo findByPage(int page, int size) {
       //1.调用PageHelper的静态方式设置参数
        PageHelper.startPage(page,size);
        //2.查询所有
        List<Company> list = companyDao.findAll();
        //3.将上个查询的list集合使用PageInfo封装
        return new PageInfo<Company>(list);
    }

}
